<?php defined('SYSPATH') or die('No direct script access.');

/**
 * 缺货通报批量上传
 * @author locove
 */
class Controller_Report_Outstock extends Controller_Admin
{
    /**
     * (non-PHPdoc)
     * @see Controller_Admin::before()
     */
    public function before()
    {
        try{
            ACL::Required('outstock');
        }catch(HTTP_Exception_403 $e){
            Message::error('sorry，你没有权限进这个目录');
            $this->request->redirect(URL::base(TRUE));
        }
        parent::before();
    }

    /**
     * 首页
     */
    public function action_index()
    {
        $this->title = '缺货通报查询';
        $view        = View::factory('report/outstock');
        if(isset($_GET['errors'])){
            $view->set('errors',$_GET['errors']);
        }
        $this->response->body($view);
    }

    /**
     * 获取分页缺货通报统计信息
     */
    public function action_outstock_load()
    {
        set_time_limit(0);
        ini_set('memory_limit','512M');
        $result         = array(); //存储结果
        $offset         = $page = 0;
        $items_per_page = 100;
        if(isset($_GET['page'])&&$_GET['page']){
            $page   = $_GET['page'];
            $offset = ($_GET['page']-1)*$items_per_page;
        }
        //START     ----------------------------------CWY更新代码-------------------------------------
        if(isset($_GET['sku'])&&$_GET['sku']){
            $skus = Skusystemapi::getSaleAndSku(trim($_GET['sku']));
        }elseif(isset($_GET['purchaser'])&&$_GET['purchaser']){
            $name = Skusystemapi::getUser(trim($_GET['purchaser']));
            if(isset($name['id'])){
                $skus = Skusystemapi::getUserAllSku($name['id']);
            }else{
                $skus = array();
            }
        }else{
            $skus = Skusystemapi::getSaleSku();
        }
        if(!empty($skus)){
            $skus_arr         = Arr::pluck($skus,'sku');
            $actual_num_all   = Warningtmp::get_inventory($skus_arr); //实际库存
            $op_print_num_all = Warningtmp::get_print_num($skus_arr); //订单占用
            $purchasers       = array();
            $users            = Skusystemapi::getAllUser();
            foreach($users as $user){
                $purchasers[$user['id']] = $user['name'];
            }
            foreach($skus as $key => $_sku){
                $sku               = $_sku['sku'];
                $actual_temp       = isset($actual_num_all[$sku])?$actual_num_all[$sku]:0;
                $op_print_num_temp = isset($op_print_num_all[$sku])?$op_print_num_all[$sku]:0;
                $keyong            = $actual_temp-$op_print_num_temp;
                if($keyong>=0){
                    continue;
                }
                $result[$key]['sku']       = $sku;
                $cnname = Skusystemapi::getSkuCnname($sku);
                $result[$key]['name']      = $cnname[0]['name'];
                $user_id = Skusystemapi::getUserSKu($sku);
                $result[$key]['purchaser'] = $purchasers[$user_id[0]['user_id']];
                $result[$key]['actual']    = $actual_temp;
                $result[$key]['zy']        = $op_print_num_temp;
                $result[$key]['ky']        = $keyong;
                $status = Skusystemapi::getSkuStatus($sku);
                $result[$key]['status']    = $status[0]['status'];
                unset($cnname);
                unset($user_id);
                unset($status);
            }
        }
//            $status   = array(
//                1,
//                3,
//                6,
//                7,
//                11
//            );
//            $skuinfos = DB::select(array(
//                DB::expr('upper(products_sku)'),
//                'sku'
//            ),'products_name_cn','status_name','oper')
//                ->from('erp_products_data')
//                ->join('erp_status')
//                ->on('erp_products_data.products_remark_2','=','erp_status.object_status')
//                ->where('products_remark_2','in',$status)
//                ->where('products_sku','!=','');
//            if(isset($_GET['sku'])&&$_GET['sku']){
//                $skuinfos->where('products_sku','=',trim($_GET['sku']));
//            }
//            if(isset($_GET['purchaser'])&&$_GET['purchaser']){
//                $sql  = "select id from erp_manages where name ='".trim($_GET['purchaser'])."'";
//                $name = DB::query(Database::SELECT,$sql)
//                    ->execute('readonlyerp')
//                    ->current();
//                if(isset($name['id'])){
//                    $skuinfos->where('oper','=',$name['id']);
//                }else{
//                    $skuinfos->where('products_id','=',0);
//                }
//            }
//            $skus = $skuinfos->execute('readonlyerp')
//                ->as_array();
//            if(!empty($skus)){
//                $skus_arr         = Arr::pluck($skus,'sku');
//                $actual_num_all   = Warningtmp::get_inventory($skus_arr); //实际库存
//                $op_print_num_all = Warningtmp::get_print_num($skus_arr); //订单占用
//                $purchasers       = array();
//                $sql              = "select id,name from erp_manages";
//                $users            = DB::query(Database::SELECT,$sql)
//                    ->execute('readonlyerp')
//                    ->as_array();
//                foreach($users as $user){
//                    $purchasers[$user['id']] = $user['name'];
//                }
//                foreach($skus as $key => $_sku){
//                    $sku               = $_sku['sku'];
//                    $actual_temp       = isset($actual_num_all[$sku])?$actual_num_all[$sku]:0;
//                    $op_print_num_temp = isset($op_print_num_all[$sku])?$op_print_num_all[$sku]:0;
//                    $keyong            = $actual_temp-$op_print_num_temp;
//                    if($keyong>=0){
//                        continue;
//                    }
//                    $result[$key]['sku']       = $sku;
//                    $result[$key]['name']      = $_sku['products_name_cn'];
//                    $result[$key]['purchaser'] = $purchasers[$_sku['oper']];
//                    $result[$key]['actual']    = $actual_temp;
//                    $result[$key]['zy']        = $op_print_num_temp;
//                    $result[$key]['ky']        = $keyong;
//                    $result[$key]['status']    = $_sku['status_name'];
//                }
//            }
            //END    ----------------------------------CWY更新代码-------------------------------------
            $count  = count($result);
            $result = array_slice($result,$offset,$items_per_page);
            $view   = View::factory('report/outstock_table')
                ->set('outstocks',$result)
                ->set('totles',$count)
                ->set('page',$page)
                ->render();
            echo $view;
        }
    }
